package edu.washington.cs.workflow.analysis;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import com.ibm.wala.ipa.callgraph.CGNode;
import com.ibm.wala.ipa.callgraph.CallGraph;

import edu.washington.cs.workflow.analysis.AnalysisBuilder.CG;
import edu.washington.cs.workflow.analysis.AnalysisBuilder.UIFramework;
import edu.washington.cs.workflow.matching.TestGanttProject;
import edu.washington.cs.workflow.matching.TestMatchingFreemind;
import edu.washington.cs.workflow.util.Utils;
import edu.washington.cs.workflow.util.WALAUtils;
import junit.framework.TestCase;

public class TestEventReachableMethod extends TestCase {

	public void testCrossword() {
		String[] pkgs = new String[]{"crosswordsage"};
		String newDir = "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\crossword-0.3.5.jar";
		String newMain = "Lcrosswordsage/MainScreen";
		
		seeAllHandlers(newDir, newMain, pkgs);
	}
	
	public void testCrossword_traverse() {
		String[] pkgs = new String[]{"crosswordsage"};
		String newDir = "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\crossword-0.3.5.jar";
		String newMain = "Lcrosswordsage/MainScreen";
		
		AnalysisBuilder builder = new AnalysisBuilder(newDir, newMain, pkgs);
		builder.setCGType(CG.RTA);
		builder.setUIFramework(UIFramework.SWING);
		builder.buildAnalysis();
		
		CGNode node = builder.findCGNodeBySignature("crosswordsage.MainScreen$MenuListener.actionPerformed(Ljava/awt/event/ActionEvent;)V");
		System.out.println(node);
		List<CGNode> nodelist = WALAUtils.getReachableAppNodes(node, builder.getCallGraph(), pkgs);
		System.out.println("Number of nodes: " + nodelist.size());
	}
	
	public void testGanttProject() {
		String[] pkgs = TestGanttProject.pkgs;
		String newDir = TestGanttProject.newDir;
		String newMain = TestGanttProject.main;
		seeAllHandlers(newDir, newMain, pkgs);
	}
	
	public void testJEdit() {
		String[] pkgs = new String[]{"org.gjt.sp."};
		String newDir = "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\jedit-2.6.jar";
		String newMain = "Lorg/gjt/sp/jedit/jEdit";
		seeAllHandlers(newDir, newMain, pkgs);
	}
	
	public void testJabRef() {
		String[] pkgs = new String[]{"net.sf.jabref."};
		String newDir = "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\jabref2.8\\JabRef-2.8.1.jar";
		String main = "Lnet/sf/jabref/JabRef";
		seeAllHandlers(newDir, main, pkgs);
	}
	
	public void testFreemind() {
		String[] pkgs = TestMatchingFreemind.pkgs;
		String newDir = TestMatchingFreemind.newDir;
		String newMain = TestMatchingFreemind.main;
		seeAllHandlers(newDir, newMain, pkgs);
	}
	
	private void seeAllHandlers(String dir, String main, String[] pkgs) {
		AnalysisBuilder builder = new AnalysisBuilder(dir, main, pkgs);
		builder.setCGType(CG.RTA);
		builder.setUIFramework(UIFramework.SWING);
		builder.buildAnalysis();
		
		EventReachableMethodFinder finder = new EventReachableMethodFinder(builder);
		Collection<CGNode> handlers = finder.getAllEventHandlers();
		System.out.println("Num of handlers: " + handlers.size());
		for(CGNode node : handlers) {
			System.out.println(node.getMethod().getSignature());
		}
	}
}
